﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Newtonsoft.Json;

namespace ProjectPacker
{
    public class UpdateInfo
    {
        public Int64 NewVersion { set; get; }
        public String NewVersionName { set; get; }
        public Int64 FileLength { set; get; }
        public String FileName { set; get; }
        public String RunPathAfterSetup { set; get; }
        public String[] DownloadList { set; get; }
        public void SetDefault()
        {
            String oldVersion = NewVersion.ToString();
            NewVersion = Convert.ToInt64(DateTime.Now.ToString("yyyyMMddHH"));
            String newVersion = NewVersion.ToString();
            NewVersionName = NewVersionName.Replace(oldVersion, newVersion);
            FileName = FileName.Replace(oldVersion, newVersion);
            for (int i = 0; i < DownloadList.Count();i++ )
            {
                DownloadList[i] = DownloadList[i].Replace(oldVersion, newVersion);
            }
        }
        public void SetDate(String Date)
        {
            String oldVersion = NewVersion.ToString();
            NewVersion = Convert.ToInt64(Date);
            String newVersion = NewVersion.ToString();
            NewVersionName = NewVersionName.Replace(oldVersion, newVersion);
            FileName = FileName.Replace(oldVersion, newVersion);
            for (int i = 0; i < DownloadList.Count(); i++)
            {
                DownloadList[i] = DownloadList[i].Replace(oldVersion, newVersion);
            }
        }
        /// <summary>
        /// Save UpdateInfo
        /// </summary>
        /// <param name="_ui">UpdateInfo</param>
        public static void Save(UpdateInfo _ui)
        {

            using (StreamWriter sw = new StreamWriter("UpdateInfo.txt"))
            {
                sw.Write(JsonConvert.SerializeObject(_ui));
            }

        }
        public static void Save(UpdateInfo _ui,String fileName)
        {

            using (StreamWriter sw = new StreamWriter(fileName))
            {
                sw.Write(JsonConvert.SerializeObject(_ui));
            }

        }
        /// <summary>
        /// Load UpdateInfo
        /// </summary>
        /// <param name="_ui">UpdateInfo referred</param>
        public static void Load(ref UpdateInfo _ui)
        {

            using (StreamReader sr = new StreamReader("UpdateInfo.Smaple.txt"))
            {
                _ui = JsonConvert.DeserializeObject<UpdateInfo>(sr.ReadToEnd());
            }

        }
        /// <summary>
        /// Load UpdateInfo
        /// </summary>
        /// <param name="_ui">UpdateInfo referred</param>
        /// <param name="fileName">File Name</param>
        public static void Load(ref UpdateInfo _ui,String fileName)
        {

            using (StreamReader sr = new StreamReader(fileName))
            {
                _ui = JsonConvert.DeserializeObject<UpdateInfo>(sr.ReadToEnd());
            }

        }
    }
}
